package com.shiyiwei.test.study.transaction.service;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Title: TransactionPropagationService
 * @Package: com.shiyiwei.test.study.transaction.service
 * @Description:
 * @author: shiyiwei
 * @date: 2022-01-06 22:18
 * @version: V1.0
 */
@Service("TransactionPropagationService2")
public class TransactionPropagationService2 {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void handleREQUIRED1() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void handleREQUIRED6() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public void handleREQUIRED2() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void handleREQUIRED3() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void handleREQUIRED4() {
        try {
            jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
//            int i = 1 / 0;
        } catch (Exception e) {
            throw new RuntimeException("报错了");
        }
    }

    @Transactional(propagation = Propagation.NEVER,rollbackFor = Exception.class)
    public void handleREQUIRED5() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
    }

    @Transactional(propagation = Propagation.NESTED,rollbackFor = Exception.class)
    public void handleREQUIRED7() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.MANDATORY,rollbackFor = Exception.class)
    public void handleREQUIRED8() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public void handleREQUIRED9() {
        jdbcTemplate.update("update account set balance=balance+100 where name = 'hanmeimei'");
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class)
    public void handleREQUIRED10() {
        jdbcTemplate.update("update account set balance=balance+100 where id = 2");
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class)
    public void handleREQUIRED11() {
        jdbcTemplate.update("update account set balance=balance+100 where id = 2");
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public void handleREQUIRED12() {
        jdbcTemplate.update("update account set balance=balance+100 where id = 1");
    }
}
